#!/bin/sh
echo "脚本用于jetson的网卡设置，设置OLED显示"

# 服务安装文件夹
sudo mkdir -p /usr/lib/systemd/system
# 更新包源
sudo apt update
# 脚本所在的目录
bashpath=$(cd `dirname $0`;pwd)

###### oled部分
sudo pip3 install Jetson.GPIO Adafruit_SSD1306 ping3 -i https://pypi.tuna.tsinghua.edu.cn/simple/
sudo apt install -y gawk
sudo groupadd -f -r gpio
sudo usermod -a -G gpio $USER   
echo "Vcc (OLED) -> 3V3 (Jetson 1脚)"
echo "GND (OLED) -> GND (Jetson 6脚)"
echo "SDA (OLED) -> I2C_SDA (Jetson 3脚)"
echo "SDL (OLED) -> I2C_SDL (Jetson 5脚)"

# 以服务安装
echo '[Unit]
Description=Oled show ip
#Before=dnsmasq.service
[Service]
#Type=oneshot
ExecStart='$bashpath'/oledShow.py
[Install]
WantedBy=multi-user.target'|sudo tee /usr/lib/systemd/system/oled.service
sudo systemctl enable oled.service
# 并直接运行
sudo systemctl restart oled.service
# 检查运行状态
# sudo systemctl status oled


###### 网卡修改
echo "jetson nano的有线网卡修改为LAN，地址192.168.3.1"
sudo apt install -y dnsmasq bridge-utils
#配置DHCP服务器
if test -f "/etc/dnsmasq.conf.back"; then
    echo "/etc/dnsmasq.conf.back exist"
else
    sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.back
fi
echo 'dhcp-range=192.168.3.50,192.168.3.150,255.255.255.0,12h'|sudo tee /etc/dnsmasq.conf #这里IP的网段跟之前配置的需要一致

# # 本次生效
# sudo $bashpath/startRouter.sh
# sudo service dnsmasq restart

#安装服务，永久生效
echo '[Unit]
Description=wan to lan as a Router
Before=dnsmasq.service
[Service]
Type=oneshot
ExecStart='$bashpath'/startRouter.sh
[Install]
WantedBy=multi-user.target'|sudo tee /usr/lib/systemd/system/router.service
sudo systemctl enable router.service
